var util = require('util');
var fs = require('fs');
var log4js = require('log4js');

/*
desc    :初始化日志
param   :options选项,
         name:模块名称,
         debug: {
            maxSize:游戏日志文件大小,
            backups:游戏日志文件数量,
         },
         error: {
            maxSize:错误日志文件大小,
            backups:错误日志文件数量,
         }
         record: {
            maxSize:记录日志文件大小,
         }
*/
exports.init = function(options) {
    var appenders = [];

    var debugOptions = options.debug;
    if (debugOptions) {
        appenders.push({
            category: 'debug',
            type: 'file',
            filename: 'log/' + options.name + '.log', 
            maxLogSize: debugOptions.maxSize || 1024*1024*256,
            backups: debugOptions.backups || 40,
            layout: {
                type: 'pattern',
                pattern: '[%d] %m',
            },
        });
    }

    var errorOptions = options.error;
    if (errorOptions) {
        appenders.push({
            category: 'error',
            type: 'file',
            filename: 'log/' + options.name + '_error.log', 
            maxLogSize: errorOptions.maxSize || 1024*1024*128,
            backups: errorOptions.backups || 8,
            layout: {
                type: 'pattern',
                pattern: '[%d] %m',
            },
        });
    }
    
    var recordOptions = options.record;
    if (recordOptions) {
        if(!fs.existsSync('log/record/')) {
            fs.mkdirSync('log/record/');
        }

        appenders.push({
            category: 'record',
            type: 'dateFile',
            filename: util.format('log/record/mdc_%s_%d', gConfGlobalServer.PlatformName||'unknown', config.ServerId),
            pattern: "_yyyyMMddhh00.log",
            alwaysIncludePattern: true,
            maxLogSize: recordOptions.maxSize || 1024*1024*1024,
            layout: {
                type: 'pattern',
                pattern: '%m',
            },
        });
    }

    var auditOptions = options.audit;
    if (auditOptions) {
        if(!fs.existsSync('log/audit/')) {
            fs.mkdirSync('log/audit/');
        }

        appenders.push({
            category: 'audit',
            type: 'dateFile',
            filename: util.format('log/audit/%d',  config.ServerId),
            pattern: "_yyyyMMdd.log",
            alwaysIncludePattern: true,
            maxLogSize: recordOptions.maxSize || 1024*1024*1024,
            layout: {
                type: 'pattern',
                pattern: '%m',
            },
        });
    }
    
    log4js.configure({'appenders': appenders});

    if (debugOptions) {
        var debugLogger = log4js.getLogger('debug');
        debugLogger.setLevel('DEBUG');

        global.LOG = function(content) {
            debugLogger.info(content);
        };
        
        global.INFO = function(content) {
            debugLogger.info(content);
        };
    }

    if (errorOptions) {
        var errorLogger = log4js.getLogger('error');
        errorLogger.setLevel('ERROR');
        
        global.ERROR = function(content) {
            errorLogger.error(content);
        };
    }

    if (recordOptions) {
        var recordLogger = log4js.getLogger('record');
        recordLogger.setLevel('DEBUG');

        global.RECORD = function(content) {
            recordLogger.info(content);
        }
    }

    if (auditOptions) {
        var auditLogger = log4js.getLogger('audit');
        recordLogger.setLevel('DEBUG');

        global.AUDIT = function(content) {
            auditLogger.info(content);
        }
    }
}
